<?php
function train(&$api, $uid)
{
    //obtain photos where this uid appears in (limit to 30)
    $urls = getTrainingUrls();
    $urls = array_splice($urls, 0, 30);

    //run face detection on all images
    $tags = $api->faces_detect($urls);
    
   
    if (empty($tags->photos))
        return false;

    //build a list of tag ids for training
    $tids = array();
    
    foreach ($tags->photos as $photo)
    {
        //skip errors
        if (empty($photo->tags))
            continue;

        //skip photos with multiple faces (want to make sure only the uid appears)
        if (count($photo->tags) > 1)
            continue;

        $tid = $photo->tags[0]->tid;
        $tids[] = $tid;
    }

        
 
    //if faces were detected, save them for this uid
    if (count($tids) > 0)
       $api->tags_save($tids, $uid, $uid);

    //train the index with the newly saved tags
    $api->faces_train($uid);
	
    
    //all done, recognition for $uid can now begin
    return true;
}

/*
    searches for uids within photos
*/
function search(&$api, $uids,$pic)
{
    //obtain list of photos to search in
    $photoUrls = getPhotoUrls($pic);
    $urls = array();
    $returnObject = new stdClass();
	    
    foreach ($photoUrls as $photoUrl)
    {
        $urls[] = $photoUrl;
        //max photos per recognition call is 30, so break photos to groups if needed
        $urls[] = $photoUrl;
        $count++;
            
        
        if (($count % 30) == 0 || $count == count($photoUrls))
        {
            $response = $api->faces_recognize($urls, $uids);
            
                       
            foreach ($response->photos as $photo)
            {
                //skip empty tags and errors
                if (empty($photo->tags))
                    continue;

                $url = $photo->url;

                //echo all found tags
                foreach ($photo->tags as $tag)
                {
				
                	if (!empty($tag->uids))
                	{
                	
                        //only interested in highest score for this tag
                        $uid = $tag->uids[0]->uid;
                        $conf = $tag->uids[0]->confidence;
							
                                                
                        $returnObject->url = $url;
                        $returnObject->uid = $uid;
                        $returnObject->confidence = $conf;
                        
                	}
                        
                                            
                }
            }
            $urls = array();
        }
    }
    
    return $returnObject;
}

function getTrainingUrls()
{
    //return an array of urls pointing to known photos of the uid\
    $photoForTrain = array();
       
    $photoForTrain[1] = 'http://sphotos.ak.fbcdn.net/hphotos-ak-snc1/hs264.snc1/9123_1243980655572_1111044925_757119_2390823_n.jpg';
    $photoForTrain[2] = 'http://sphotos.ak.fbcdn.net/hphotos-ak-ash1/hs448.ash1/24676_1350117908937_1111044925_1047474_506154_n.jpg';
    $photoForTrain[3] = 'http://sphotos.ak.fbcdn.net/hphotos-ak-snc3/hs523.snc3/29762_1422654602309_1111044925_1225742_8155843_n.jpg';
    $photoForTrain[4] = 'http://sphotos.ak.fbcdn.net/hphotos-ak-ash1/hs280.ash1/20756_1328247282185_1111044925_992485_6186607_n.jpg';
    $photoForTrain[5] = 'http://sphotos.ak.fbcdn.net/hphotos-ak-sjc1/hs294.snc3/28349_1413700418460_1111044925_1208504_7420614_n.jpg';
    $photoForTrain[6] = 'http://sphotos.ak.fbcdn.net/hphotos-ak-sjc1/hs534.snc3/30322_1403325239087_1111044925_1185147_983737_n.jpg';
    $photoForTrain[7] = 'http://sphotos.ak.fbcdn.net/hphotos-ak-sjc1/hs235.snc3/22256_1292015336409_1111044925_889205_4799302_n.jpg';
    $photoForTrain[8] = 'http://sphotos.ak.fbcdn.net/photos-ak-snc1/v1688/16/7/1111044925/n1111044925_233281_1313.jpg';
    $photoForTrain[9] = 'http://sphotos.ak.fbcdn.net/photos-ak-snc1/v787/16/7/1111044925/n1111044925_206335_6905.jpg';
    
    	
    return $photoForTrain;
}

function getPhotoUrls($pic)
{
		
		$photoForTest = array();
		if($pic == 1){
	   		   $photoForTest[0] = 'http://sphotos.ak.fbcdn.net/hphotos-ak-snc3/hs239.snc3/22656_1310583840610_1111044925_945783_4194368_n.jpg'; 
		}else{
	   		   $photoForTest[0] = 'http://avatars.plurk.com/3154667-big4.jpg'; 
		}
		
	   return $photoForTest;
}

?>

